package com.sunday.pinxinghui.model.order;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.common.base.BaseActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.model.index.IndexClasssifyFragment;
import com.sunday.pinxinghui.model.index.TodayFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.support.design.widget.TabLayout.MODE_SCROLLABLE;

/**
 * Created by 刘涛 on 2017/8/10.
 */

public class OrderListActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;


    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        titleView.setText("我的订单");
        initView();
    }

    private void initView() {
        tabLayout.setTabMode(MODE_SCROLLABLE);
        titles.add("全部");
        titles.add("待付款");
        titles.add("待发货");
        titles.add("待收货");
        titles.add("待提货");
//        titles.add("售后");
        titles.add("待评价");
        viewPager.setOffscreenPageLimit(6);
        viewPager.setAdapter(new OrderPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(MODE_SCROLLABLE);
        int page = getIntent().getIntExtra("page", 0);
        viewPager.setCurrentItem(page, false);

    }

    public class OrderPagerAdapter extends FragmentPagerAdapter {

        private Fragment[] fragments;

        public OrderPagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                switch (position) {
                    case 0:
                        fragments[position] = OrderListFragment.newInstance(-1);
                        break;
                    case 1:
                        fragments[position] = OrderListFragment.newInstance(0);
                        break;
                    case 2:
                        fragments[position] = OrderListFragment.newInstance(1);
                        break;
                    case 3:
                        //待收货
                        fragments[position] = OrderListFragment.newInstance(2);
                        break;
                    case 4:
                        //待提货
                        fragments[position] = OrderListFragment.newInstance(11);
                        break;
//                    case 5:
//                        //售后
//                        fragments[position] = OrderListFragment.newInstance(5);
//                        break;
                    case 5:
                        //待评价 已收货
                        fragments[position] = OrderListFragment.newInstance(3);
                        break;
                }
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}
